// pages/todolist/todolist.js
const db = wx.cloud.database()
const list = db.collection('list')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        isDoneList: [],
        notIsDoneList: [],
        delete_id: '',
        delete_state: '',
        update_task: {},
        new_task: {}
    },

    /**
     * 导航到详情页面
     */
    gotoDetail(e) {
        var id = e.currentTarget.dataset.id
        var openid = e.currentTarget.dataset.openid
        var date = e.currentTarget.dataset.date
        var discription = e.currentTarget.dataset.discription
        var state = e.currentTarget.dataset.state
        var task = e.currentTarget.dataset.task
        wx.navigateTo({
            url: `/pages/detail/detail?_id=${id}&_openid=${openid}&date=${date}&discription=${discription}&state=${state}&task=${task}`,
        })
    },

    /**
     * 导航到新建事项页面
     */
    gotoNewtodo() {
        wx.navigateTo({
            url: '/pages/newtodo/newtodo',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var that = this
        // 获取内容
        wx.cloud.callFunction({
            name: 'getDoneList',
            data: {
                state: '已完成'
            },
            success: function (res) {
                that.setData({
                    isDoneList: res.result.data
                })
            }
        })
        wx.cloud.callFunction({
            name: 'getDoneList',
            data: {
                state: '未完成'
            },
            success: function (res) {
                that.setData({
                    notIsDoneList: res.result.data
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var that = this
        if (this.data.delete_id != '') {
            if (this.data.delete_state == '已完成') {
                that.data.isDoneList.splice(that.data.isDoneList.findIndex(element => element._id === this.data.delete_id), 1)
                this.setData({
                    isDoneList: that.data.isDoneList
                })
            } else {
                that.data.notIsDoneList.splice(that.data.notIsDoneList.findIndex(element => element._id === this.data.delete_id), 1)
                this.setData({
                    notIsDoneList: that.data.notIsDoneList
                })
            }
        }
        if (this.data.update_task != '') {
            if (this.data.update_task.state === '未完成') {
                var index = that.data.notIsDoneList.findIndex(element => element._id === this.data.update_task._id)
                that.data.notIsDoneList[index] = this.data.update_task
                this.setData({
                    notIsDoneList: that.data.notIsDoneList
                })
            } else {
                var index = that.data.isDoneList.findIndex(element => element === this.data.update_task)
                that.data.isDoneList[index] = this.data.update_task
                this.setData({
                    isDoneList: that.data.isDoneList
                })
            }
        }
        if (this.data.new_task != '') {
            if (this.data.new_task.state === '未完成') {
                that.data.notIsDoneList.push(this.data.new_task)
                this.setData({
                    notIsDoneList: that.data.notIsDoneList
                })
            } else {
                that.data.isDoneList.push(this.data.new_task)
                this.setData({
                    isDoneList: that.data.isDoneList
                })
            }
        }
        this.setData({
            delete_id: '',
            delete_state: '',
            update_id: '',
            new_task: ''
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})